//
//  RepairHistoryViewController.m
//  ICrEAM
//
//  Created by xphone on 16/11/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "RepairHistoryViewController.h"
#import "ICMHeaderView.h"
#import "LocalRepairHistoryView.h"
#import "NationRepairHistoryView.h"
#import "ICMRepairOrderModel.h"
#import "ICMRepairMaintenanceModel.h"
#import "ICMRepairPartsModel.h"

//车主车辆信息列表查询

@interface RepairHistoryViewController () <UITableViewDataSource,UITableViewDelegate,ICMHeaderViewDelegate,UITextFieldDelegate>{
    NSString *_vinText;
}

@property (nonatomic, strong) UIScrollView *bigScrollView;

@property (nonatomic, strong) ICMHeaderView *headerView;

@property (nonatomic, strong) NSMutableArray *searchNationArray;

@property (nonatomic, strong) LocalRepairHistoryView *localView;

@property (nonatomic, strong) NationRepairHistoryView *nationView;

@property (nonatomic, assign) BOOL isLocal;//是否为本地

@property (nonatomic, weak) UITextField * searchText;

@end

@implementation RepairHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackgroundColor_HeaderBlue;
    [self setupBigScrollView];
    [self setupHeadView];
    [self setupSearchView];
}

#pragma mark 配置tableview
- (void)setupBigScrollView
{
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake( 0, ActualHeight(40), ScreenWidth, ScreenHeight-NavBarAndStatusHeight-ActualHeight(40))];
    self.bigScrollView.contentSize = CGSizeMake(ScreenWidth*2,ScreenHeight-NavBarAndStatusHeight-ActualHeight(40));
    self.bigScrollView.backgroundColor = BackgroundColor_White;
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.delegate = self;
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    
    self.localView = [LocalRepairHistoryView createLocalRepairHistoryViewWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.nationView = [NationRepairHistoryView createNationRepairHistoryViewWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    
    [self.bigScrollView addSubview:self.localView];
    [self.bigScrollView addSubview:self.nationView];
    [self.view addSubview:self.bigScrollView];
    
}

#pragma mark 配置headView
- (void)setupHeadView
{
    self.headerView = [ICMHeaderView createHeaderViewWithFrame:CGRectMake(ScreenWidth/10, 0, ScreenWidth-ActualWidth(400)-ScreenWidth/4, ActualHeight(40)) andTitles:@[@"本地维修历史查询",@"全国维修历史查询"] andType:@"0"];
    self.headerView.delegate = self;
    [self.view addSubview:self.headerView];
}

#pragma mark 配置搜索框
- (void)setupSearchView
{
    //搜索框
    UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth-ActualWidth(340), ActualHeight(4), ActualWidth(300), ActualHeight(32))];
    
    searchView.backgroundColor = [UIColor whiteColor];
    searchView.layer.cornerRadius = ActualHeight(32)/2.0;
    
    UIImageView *searchImage = [[UIImageView alloc]initWithFrame:CGRectMake(ActualWidth(4), ActualHeight(2), ActualWidth(28), ActualHeight(28))];
    searchImage.image = [UIImage imageNamed:GetCar_SearchIcon];
    [searchView addSubview:searchImage];
    
    UITextField * searchText = [[UITextField alloc]initWithFrame:CGRectMake(ActualWidth(32), 0, ActualWidth(260)-ActualWidth(32), ActualHeight(32))];
    searchText.placeholder = @"VIN、车牌号";
    searchText.delegate = self;
    searchText.returnKeyType = UIReturnKeySearch;
    searchText.font = [UIFont systemFontOfSize:14];
    [searchView addSubview:searchText];
    if (!IsStrEmpty(_vinText)) {
        searchText.text=_vinText;
    }
    self.searchText = searchText;
  
    [self.view addSubview:searchView];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:searchText];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float offsetX = scrollView.contentOffset.x;
    self.headerView.selectedIndex = offsetX/ScreenWidth;
    if (self.headerView.selectedIndex == 0) {
        self.searchText.placeholder = @"VIN、车牌号";
        if (!IsStrEmpty(  self.searchText.text)) {
            [self localRepairOrderWithCondition:self.searchText.text];
        }
    } else {
        self.searchText.placeholder = @"VIN";
        if (!IsStrEmpty(  self.searchText.text)) {
            [self nationRepairOrderWithCondition:self.searchText.text];
        }
    }
}

#pragma mark -- ICMHeaderViewDelegate
- (void)headerViewDidSelectedButtonAtIndex:(NSInteger)index{
    [self.bigScrollView scrollRectToVisible:CGRectMake(ScreenWidth*index, 0, ScreenWidth, ScreenHeight-NavBarAndStatusHeight-ActualHeight(40)) animated:YES];
}

#pragma mark -- UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.headerView.selectedIndex == 0) {
        //校验
        if (textField.text.length > 0) {
            if ([self IsChinese:[textField.text substringToIndex:1]]) {
                if (textField.text.length == 7) {
                    //查询本地的数据
                    [self localRepairOrderWithCondition:textField.text];
                } else {
                    [self.view showString:@"请输入完整的车牌号或者VIN码进行搜索！" forSeconds:1.5];
                }
            } else {
                if (textField.text.length == 17) {
                    //查询本地的数据
                    [self localRepairOrderWithCondition:textField.text];
                } else {
                    [self.view showString:@"请输入完整的车牌号或者VIN码进行搜索！" forSeconds:1.5];
                }
            }
        }
        
    }else{
        //校验
        if (textField.text.length == 17) {
            //查询全国的数据
            [self nationRepairOrderWithCondition:textField.text];
        } else {
            [self.view showString:@"请输入完整的VIN码进行搜索！" forSeconds:1.5];
        }
    }
    [textField resignFirstResponder];
    return YES;
}

- (void)textFiledEditChanged:(NSNotification *)obj {
    UITextField *textField = obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            textField.text= toBeString.uppercaseString;
        }else{
            // 有高亮选择的字符串
        }
    }else {
        textField.text = textField.text.uppercaseString;
    }
}


-(BOOL)IsChinese:(NSString *)str {
    for(int i=0; i< [str length];i++){
        int a = [str characterAtIndex:i];
        if( a > 0x4e00 && a < 0x9fff)
        {
            return YES;
        }
        
    }
    return NO;
}

#pragma mark -- 查询接口本地

- (void)localRepairOrderWithCondition:(NSString *)condition {
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:condition forKey:@"QUERY_CONDITION"];
    //[parameters setObject:@1 forKey:@"CURRENT_PAGE"];
    [manager startGetRequestWithURL:RepairOrder parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            ICMRepairOrderArrayModel *orderArraymodel = [ICMRepairOrderArrayModel yy_modelWithDictionary:data];
            [orderArraymodel createRepairOrderArrayModel];
            
            for (ICMRepairOrderModel *model in orderArraymodel.TT_REPAIR_ORDER) {
         
                NSMutableArray *array1 = [NSMutableArray array];
                for (NSDictionary *dic in model.TT_RO_LABOUR) {
                    ICMRepairMaintenanceModel *model1 = [ICMRepairMaintenanceModel yy_modelWithDictionary:dic];
                    [array1 addObject:model1];
                }
                model.TT_RO_LABOUR = array1;
                
                NSMutableArray *array2 = [NSMutableArray array];
                for (NSDictionary *dic in model.TT_RO_REPAIR_PART) {
                    ICMRepairPartsModel *model2 = [ICMRepairPartsModel yy_modelWithDictionary:dic];
                    [array2 addObject:model2];
                }
                model.TT_RO_REPAIR_PART = array2;
            }
            self.localView.condition = condition;
          
            //刷新
            [self.localView reloadLocalData:orderArraymodel];
        }
    }];
}

#pragma mark -- 查询接口全国
- (void)nationRepairOrderWithCondition:(NSString *)condition {
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:condition forKey:@"VIN"];
    //[parameters setObject:@1 forKey:@"CURRENT_PAGE"];
    [manager startGetRequestWithURL:Maintenance parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            ICMRepairOrderArrayModel *orderArraymodel = [ICMRepairOrderArrayModel yy_modelWithDictionary:data];
            [orderArraymodel createRepairOrderArrayModel];
            
            if(orderArraymodel.TT_REPAIR_ORDER.count>0){
                self.nationView.nationTableView.hidden = NO;
                self.nationView.nodataLabel.hidden = YES;
                for (ICMRepairOrderModel *model in orderArraymodel.TT_REPAIR_ORDER) {
                    NSMutableArray *array1 = [NSMutableArray array];
                    for (NSDictionary *dic in model.TT_RO_LABOUR) {
                        ICMRepairMaintenanceModel *model1 = [ICMRepairMaintenanceModel yy_modelWithDictionary:dic];
                        [array1 addObject:model1];
                    }
                    model.TT_RO_LABOUR = array1;
                    
                    NSMutableArray *array2 = [NSMutableArray array];
                    for (NSDictionary *dic in model.TT_RO_REPAIR_PART) {
                        ICMRepairPartsModel *model2 = [ICMRepairPartsModel yy_modelWithDictionary:dic];
                        [array2 addObject:model2];
                    }
                    model.TT_RO_REPAIR_PART = array2;
                }
                self.nationView.condition = condition;

            
            }else{
                self.nationView.nodataLabel.hidden = NO;
                self.nationView.nationTableView.hidden = YES;
            
            
            }
            
            
            
            
            
                        //刷新
            [self.nationView reloadLocalData:orderArraymodel];
        }
    }];
}

#pragma mark 用于头部查询
- (void)superSearchWithKeyword:(NSString *)str
{

    _vinText=str;

    [self localRepairOrderWithCondition:str];

}


@end
